package com.shop.util;

import java.sql.SQLException;

public class TransactionManager {

    /**
     * 开启事务
     */
    public static void beginTransaction(){
        try {
            JDBCUtils.getConnection().setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 提交事务
     */
    public static void commit(){
        try {
            JDBCUtils.getConnection().commit();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 回滚事务
     */
    public static void rollback(){
        try {
            JDBCUtils.getConnection().rollback();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 释放资源
     */
    public static void release(){
        try {
            JDBCUtils.getConnection().close();
            JDBCUtils.removeConn();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

}
